package com.sinaapp.gavinzhang.jianzhioffer;

import org.junit.Test;

/**
 * 输入一个链表，反转链表后，输出链表的所有元素。
 * Created by gavin on 15-9-4.
 */
public class ReverseList {

    @Test
    public void test()
    {
        ListNode listNode = new ListNode(1);
        listNode.next = new ListNode(2);
        listNode.next.next = new ListNode(3);
        listNode.next.next.next = new ListNode(4);

        ReverseList(listNode);
        System.out.println();
    }

    public static ListNode ReverseList(ListNode head) {
        if (head == null)return null;
        if (head.next == null)return head;
        ListNode listNode = new ListNode(1);


        while (head.next != null)
        {
            ListNode tmp = head.next;
            ListNode tmp2 = listNode.next;
            listNode.next = head;
            listNode.next.next = tmp2;
            head = tmp;
        }
        listNode.val = head.val;

        return listNode;
    }




    static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}



